package com.inter.law.dao;

import java.sql.Timestamp;
import java.util.List;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.inter.law.entity.IlLawer;

/**
 * A data access object (DAO) providing persistence and search support for
 * IlLawer entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.inter.law.dao.IlLawer
 * @author MyEclipse Persistence Tools
 */

public class IlLawerDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory.getLogger(IlLawerDAO.class);
	// property constants
	public static final String LAWER_WORKNUMBER = "lawerWorknumber";
	public static final String LAWER_USERNAME = "lawerUsername";
	public static final String LAWER_IDENTIFY = "lawerIdentify";
	public static final String LAWER_PASSWORD = "lawerPassword";
	public static final String LAWER_PHONE = "lawerPhone";
	public static final String LAWER_NAME = "lawerName";
	public static final String LAWER_AGE = "lawerAge";
	public static final String LAWER_SEX = "lawerSex";
	public static final String LAWER_EMAIL = "lawerEmail";
	public static final String LAWER_IDNUMBER = "lawerIdnumber";
	public static final String LAWER_IDCARDNUMBER = "lawerIdcardnumber";
	public static final String LAWER_FIRM = "lawerFirm";
	public static final String LAWER_PROVINCE = "lawerProvince";
	public static final String LAWER_CITY = "lawerCity";
	public static final String LAWER_MANAGER = "lawerManager";
	public static final String LAWER_LOCATION = "lawerLocation";
	public static final String LAWER_DEPT_ID = "lawerDeptId";
	public static final String LAWER_DUTY = "lawerDuty";
	public static final String LAWER_MAJOR = "lawerMajor";
	public static final String LAWER_PINYIN_NAME = "lawerPinyinName";

	protected void initDao() {
		// do nothing
	}

	public void save(IlLawer transientInstance) {
		log.debug("saving IlLawer instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(IlLawer persistentInstance) {
		log.debug("deleting IlLawer instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public IlLawer findById(java.lang.Integer id) {
		log.debug("getting IlLawer instance with id: " + id);
		try {
			IlLawer instance = (IlLawer) getHibernateTemplate().get(
					"com.inter.law.entity.IlLawer", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(IlLawer instance) {
		log.debug("finding IlLawer instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding IlLawer instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from IlLawer as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	
	/**
	 * 通过姓名和部门查询，其中姓名是模糊匹配，返回条目总数
	 * 
	 * @param propertyName
	 * @param value
	 * @return
	 */
	public Integer findByNameAndDeptTotal(String name, Integer dept) {
		try {
			String queryString = "select count(*) from IlLawer as model ";
			if (dept == null || dept == 0) {
				if (name == null || name.trim().equals("")) {

				} else {
					queryString += "where  model.lawerName like '" + name + "%'";
				}

			} else {
				if (name == null || name.trim().equals("")) {
					queryString += "where  model.lawerDeptId=" + dept;
				} else {
					queryString += "where  model.lawerName like '" + name + "%' and model.lawerDeptId=" + dept;
				}
			}
			Long numberL = (Long) getHibernateTemplate().find(queryString)
					.listIterator().next();
			long numberl=numberL;
			int counti=(int)numberl;
			Integer count=counti;
			return count;
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/**
	 * 通过姓名和部门查询，其中姓名是模糊匹配 分页查询
	 * 
	 * @param propertyName
	 * @param value
	 * @return
	 */
	public List findByNameAndDept(String name, Integer dept, Integer intPage,
			Integer pageSize) {
		try {
			String queryString = "from IlLawer as model ";
		
			if (dept == null || dept == 0) {
				if (name == null || name.trim().equals("")) {

				} else {
					queryString += "where  model.lawerName like '" + name + "%'";
				}

			} else {
				if (name == null || name.trim().equals("")) {
					queryString +="where  model.lawerDeptId=" + dept;
				} else {
					queryString += "where  model.lawerName like '" + name + "%' and model.lawerDeptId=" + dept;
				}
			}
			 Query query = getSession().createQuery(queryString); 
		     query.setFirstResult((intPage-1)*pageSize);
		     query.setMaxResults(pageSize);
		     
		     List list = query.list();  
		   
			return list;	
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	
	public List findByNameAndManager(String name, Boolean manager, Integer intPage,
			Integer pageSize) {
		try {
			String queryString = "from IlLawer as model ";
		
			if (manager == null) {
				if (name == null || name.trim().equals("")) {
					
				} else {
					queryString += "where  model.lawerName like '" + name + "%'";
				}

			} else {
				if (name == null || name.trim().equals("")) {
					queryString += "where  model.lawerManager=" + manager;
				} else {
					queryString += "where  model.lawerName like '" + name + "%' and model.lawerManager="
							+manager;
				}
			}
			 Query query = getSession().createQuery(queryString); 
		     query.setFirstResult((intPage-1)*pageSize);
		     query.setMaxResults(pageSize);
		     
		     List list = query.list();   
			return list;	
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	
	public List findByNameAndPinyin(String name, Boolean manager, Integer intPage,
			Integer pageSize) {
		try {
			String queryString = "from IlLawer as model ";
		
			if (manager == null) {
				if (name == null || name.trim().equals("")) {
					
				} else {
					queryString += "where  model.lawerPinyinName like '" + name + "%'";
				}

			} else {
				if (name == null || name.trim().equals("")) {
					queryString += "where  model.lawerManager=" + manager;
				} else {
					queryString += "where  model.lawerPinyinName like '" + name + "%' and model.lawerManager="
							+manager;
				}
			}
			 Query query = getSession().createQuery(queryString); 
		     query.setFirstResult((intPage-1)*pageSize);
		     query.setMaxResults(pageSize);
		     
		     List list = query.list();   
			return list;	
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByLawerWorknumber(Object lawerWorknumber) {
		return findByProperty(LAWER_WORKNUMBER, lawerWorknumber);
	}

	public List findByLawerUsername(Object lawerUsername) {
		return findByProperty(LAWER_USERNAME, lawerUsername);
	}

	public List findByLawerIdentify(Object lawerIdentify) {
		return findByProperty(LAWER_IDENTIFY, lawerIdentify);
	}

	public List findByLawerPassword(Object lawerPassword) {
		return findByProperty(LAWER_PASSWORD, lawerPassword);
	}

	public List findByLawerPhone(Object lawerPhone) {
		return findByProperty(LAWER_PHONE, lawerPhone);
	}

	public List findByLawerName(Object lawerName) {
		return findByProperty(LAWER_NAME, lawerName);
	}

	public List findByLawerAge(Object lawerAge) {
		return findByProperty(LAWER_AGE, lawerAge);
	}

	public List findByLawerSex(Object lawerSex) {
		return findByProperty(LAWER_SEX, lawerSex);
	}

	public List findByLawerEmail(Object lawerEmail) {
		return findByProperty(LAWER_EMAIL, lawerEmail);
	}

	public List findByLawerIdnumber(Object lawerIdnumber) {
		return findByProperty(LAWER_IDNUMBER, lawerIdnumber);
	}

	public List findByLawerIdcardnumber(Object lawerIdcardnumber) {
		return findByProperty(LAWER_IDCARDNUMBER, lawerIdcardnumber);
	}

	public List findByLawerFirm(Object lawerFirm) {
		return findByProperty(LAWER_FIRM, lawerFirm);
	}

	public List findByLawerProvince(Object lawerProvince) {
		return findByProperty(LAWER_PROVINCE, lawerProvince);
	}

	public List findByLawerCity(Object lawerCity) {
		return findByProperty(LAWER_CITY, lawerCity);
	}

	public List findByLawerManager(Object lawerManager) {
		return findByProperty(LAWER_MANAGER, lawerManager);
	}

	public List findByLawerLocation(Object lawerLocation) {
		return findByProperty(LAWER_LOCATION, lawerLocation);
	}

	public List findByLawerDeptId(Object lawerDeptId) {
		return findByProperty(LAWER_DEPT_ID, lawerDeptId);
	}

	public List findByLawerDuty(Object lawerDuty) {
		return findByProperty(LAWER_DUTY, lawerDuty);
	}

	public List findByLawerMajor(Object lawerMajor) {
		return findByProperty(LAWER_MAJOR, lawerMajor);
	}

	public List findByLawerPinyinName(Object lawerPinyinName) {
		return findByProperty(LAWER_PINYIN_NAME, lawerPinyinName);
	}

	public List findAll() {
		log.debug("finding all IlLawer instances");
		try {
			String queryString = "from IlLawer";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public IlLawer merge(IlLawer detachedInstance) {
		log.debug("merging IlLawer instance");
		try {
			IlLawer result = (IlLawer) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(IlLawer instance) {
		log.debug("attaching dirty IlLawer instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(IlLawer instance) {
		log.debug("attaching clean IlLawer instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static IlLawerDAO getFromApplicationContext(ApplicationContext ctx) {
		return (IlLawerDAO) ctx.getBean("IlLawerDAO");
	}
}